package com.itheima.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.itheima.constant.MessageConstant;
import com.itheima.entity.PageResult;
import com.itheima.entity.QueryPageBean;
import com.itheima.entity.Result;
import com.itheima.pojo.Permission;
import com.itheima.service.PermissionService;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("/permission")
public class PermissionController {
     @Reference
    private PermissionService permissionService;

     @RequestMapping("/findPage")
    private Result findPage(@RequestBody QueryPageBean queryPageBean){

         try {
             PageResult pageResult = permissionService.findPage(queryPageBean);

             return new Result(true,MessageConstant.QUERY__PERMISSION_SUCCESS,pageResult);
         } catch (Exception e) {
             e.printStackTrace();
             return new Result(false, MessageConstant.QUERY__PERMISSION_FAIL);
         }
     }
    @RequestMapping("/add")
    private Result add(@RequestBody Permission permission){
        try {
                permissionService.add(permission);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,MessageConstant.ADD__PERMISSION_FAIL);
        }
        return  new Result(true,MessageConstant.ADD__PERMISSION_SUCCESS);
    }
    // 编辑需要数据回显
    @RequestMapping("/findById")
    private Result findById(Integer id){
        try {
            Permission permission = permissionService.findById(id);
            return  new Result(true,MessageConstant.QUERY__PERMISSION_SUCCESS,permission);
        } catch (Exception e) {
            e.printStackTrace();
          return new Result(false,MessageConstant.QUERY__PERMISSION_SUCCESS);
        }
    }
    // 编辑权限
    @RequestMapping("/edit")
    private Result edit(@RequestBody Permission permission){
        try {
            permissionService.edit(permission);
        } catch (Exception e) {
            e.printStackTrace();
          return new Result(false,MessageConstant.EDIT__PERMISSION_FAIL);
        }
        return  new Result(true,MessageConstant.EDIT__PERMISSION_SUCCESS);
      }
    // 删除权限
    @RequestMapping("/delete")
    private Result delete(Integer id) {

        try {
            permissionService.delete(id);

        }
        catch(RuntimeException e ){
            return new Result(false, e.getMessage());
        }
         catch (Exception e) {
            e.printStackTrace();
            return  new Result(false,MessageConstant.DELETE__PERMISSION_FAIL);
        }
        return  new Result(true,MessageConstant.DELETE__PERMISSION_SUCCESS);

    }
}